#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2019/2/20 15:41
@desc
"""

from concurrent.futures import ThreadPoolExecutor
tpool = ThreadPoolExecutor(max_workers=1, thread_name_prefix='syn-sub-task')


class TaskExecutor:

    def __init__(self, log):
        self.log = log

    def handler_task(self, task, **kwargs):
        ret = ('255', task.task_id, task.task_name)
        try:
            ret = task.task_run()
        except BaseException as e:
            self.log.error(e.message)
            pass
        return ret

    def execute_task(self, schdl_tasks):
        """
        向线程池提交执行任务
        :param schdl_tasks tuple:
        :return: list
        """
        task_stat_results = []
        for t in schdl_tasks:
            trd = tpool.submit(self.handler_task, t)
            task_stat_results.append(trd)
        return task_stat_results
